package com.chen.manager.service.impl;

import com.chen.manager.mapper.CategoryBrandMapper;
import com.chen.manager.service.CategoryBrandService;
import com.chen.model.dto.product.CategoryBrandDto;
import com.chen.model.entity.product.CategoryBrand;
import com.chen.model.vo.common.ResultCodeEnum;
import com.chen.service.exception.MyException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cgj
 * @date 3/8/25 9:24 AM
 */
@Service
@Transactional
public class CategoryBrandServiceImpl implements CategoryBrandService {
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Transactional
    @Override
    public PageInfo<CategoryBrand> showByPageAndCidPid(Integer pageNum, Integer pageSize, CategoryBrandDto categoryBrandDto) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<CategoryBrand> categoryBrands = categoryBrandMapper.selectByCategoryBrandDto(categoryBrandDto);
        return new PageInfo<>(categoryBrands);
    }

    /**
     * 新增品牌分类
     * 防止用户添加相同的数据
     * @param categoryBrandDto
     */
    @Override
    public void sava(CategoryBrandDto categoryBrandDto) {
        //根据参数去查,当前存在不存在记录
        Long count=categoryBrandMapper.selectCount(categoryBrandDto);
        if(count.intValue()>0){
            //数据库已经有要添加的数据了,直接退出
           throw  new MyException(ResultCodeEnum.REPETITION_DATA);
        }
        categoryBrandMapper.insertCategoryBrand(categoryBrandDto);

    }

    /**
     * 修改品牌分类
     * @param categoryBrandDto
     */
    @Override
    public void editCategoryBrand(CategoryBrandDto categoryBrandDto) {
        categoryBrandMapper.updateCategoryBrand(categoryBrandDto);
    }

    /**
     * 删除品牌分类
     * @param id  根据id删除
     */
    @Override
    public void delCategoryBrand(Long id) {
        categoryBrandMapper.delCategoryBrand(id);
    }
}
